package com.brilliance.project.common.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.brilliance.project.common.exception.BaseJasperReportException;
import com.brilliance.project.common.exception.ErrorReferenceReport;

public class ExcelReportExportImpl implements IBaseReportExport {
	private static Log log = LogFactory.getLog(ExcelReportExportImpl.class);

	// 临时文件的路径
	private String tempPath = ServletActionContext.getServletContext()
			.getRealPath(File.separator) + "temp" + File.separator;

	public String jasperreportFilePath = null;

	public void setJasperreportFilePath(String jasperreportFilePath) {
		this.jasperreportFilePath = jasperreportFilePath;
	}

	/**
	 * 利用报表使用JExcel生成xls报表
	 * 
	 * @return
	 */
	protected JRXlsAbstractExporter getXlsExporter() {
		return new JExcelApiExporter();
	}

	@Override
	public void export(JasperPrint jasperPrint, String fileName) {
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(fileName);
		} catch (FileNotFoundException e) {
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_OUTPUTSTREAM_ERROR, e);
		}

		try {
			// 使用JRPdfExproter导出excel报表
			JRXlsExporter exporter = new JRXlsExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
					outputStream);
			exporter.exportReport();
		} catch (Exception ex) {
			log.error(ex.toString(), ex);
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_PRINTWRITER_ERROR);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException ex) {
					log.error(ex.toString(), ex);
				}
			}
		}
	}

	@Override
	public void export(List<JasperPrint> jasperPrintList, String fileName,
			String[] sheetNames) {
		OutputStream outputStream = null;

		try {
			outputStream = new FileOutputStream(fileName);
		} catch (FileNotFoundException e) {
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_OUTPUTSTREAM_ERROR, e);
		}

		try {
			// 使用JRPdfExproter导出excel报表
			JRXlsExporter exporter = new JRXlsExporter();
			// exporter.setParameter(JRExporterParameter.JASPER_PRINT,
			// jasperPrint);
			exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,
					jasperPrintList);
			exporter.setParameter(JRXlsAbstractExporterParameter.SHEET_NAMES,
					sheetNames);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
					outputStream);
			exporter.exportReport();
		} catch (Exception ex) {
			log.error(ex.toString(), ex);
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_PRINTWRITER_ERROR);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException ex) {
					log.error(ex.toString(), ex);
				}
			} /* end of if */
		} /* end of try ... catch ... finally */
	}

	@Override
	public InputStream exportForInputStream(JasperPrint jasperPrint,
			String fileName) {
		fileName = tempPath + fileName;
		FileInputStream fis = null;
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(fileName);
		} catch (FileNotFoundException e) {
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_OUTPUTSTREAM_ERROR, e);
		}

		try {
			// 使用JRPdfExproter导出excel报表
			JRXlsExporter exporter = new JRXlsExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
					outputStream);
			exporter.exportReport();

			File file = new File(fileName);
			fis = new FileInputStream(file);
		} catch (Exception ex) {
			log.error(ex.toString(), ex);
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_PRINTWRITER_ERROR);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException ex) {
					log.error(ex.toString(), ex);
				}
			}
		}
		return fis;
	}

}
